/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.repository;

import com.bifrost.entity.MerchantStore;
import com.bifrost.entity.Product;
import com.bifrost.entity.ProductSku;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @author joewee
 * @version 1.0.0
 * @Date 2017/10/30 20:44
 */
public interface ProductSkuRepository extends JpaRepository<ProductSku, Long>, JpaSpecificationExecutor<ProductSku> {

    /**
     * 查询所有商品最底层类别
     *
     * @return List
     */
    @Query(value = "SELECT DISTINCT SKU.CATEGORY_ID FROM PRODUCT_SKU SKU", nativeQuery = true)
    List<BigInteger> getDepthCategoryId();

    /**
     * 查询所有商品最底层类别
     *
     * @return List
     */
    @Query(value = "SELECT DISTINCT SKU.CATEGORY_ID FROM PRODUCT_SKU SKU WHERE SKU.MERCHANT_ID = ?1", nativeQuery = true)
    List<BigInteger> getDepthCategoryIdByMerchantStore(Long merchantId);

    @Query(value = "select sku from ProductSku  sku where skuid in ?1")
    List<ProductSku> getAllBySkuIdIn(List<Long> skus);

    /**
     * 增加库存
     * @param skuId
     * @param quantity
     * @return
     */
    @Modifying
    @Transactional
    @Query(value = "UPDATE PRODUCT_SKU SET QUANTITY_AVAILABLE = QUANTITY_AVAILABLE + ?2 WHERE SKU_ID = ?1",nativeQuery = true)
    int incrementInventory(Long skuId ,Integer quantity);

    /**
     * 扣减库存
     * @param skuId
     * @param quantity
     * @return
     */
    @Modifying
    @Transactional
    @Query(value = "UPDATE PRODUCT_SKU SET QUANTITY_AVAILABLE = QUANTITY_AVAILABLE - ?2 WHERE SKU_ID = ?1 AND QUANTITY_AVAILABLE >= ?2",nativeQuery = true)
    int decrementInventory(Long skuId ,Integer quantity);

    /**
     * 查询商品所有库存量
     * @param product
     * @return
     */
    @Query(value = "SELECT SUM(QUANTITY_AVAILABLE) FROM PRODUCT_SKU WHERE PRODUCT_ID = ?1 AND MERCHANTABILITY = 1",nativeQuery = true)
    Integer getProductInventory(Long product);
    ProductSku findFirstByExternalId(String externalId);

    ProductSku findFirstByExternalIdAndMerchantId(String externalId , Long merchantId);

    ProductSku findFirstBySkuCode(String skuCode);

    // 查找门店下对应Sap编码Sku；
    List<ProductSku> findAllByExternalIdAndMerchantIdIn(String externalId , Collection<Long> merchantIds);
    List<ProductSku> findAllByExternalIdAndMerchantId(String externalId , Long merchantId);


    /**
     * 批量修改热门商品
     * @param pids
     * @param hot
     * @return
     */
    @Transactional
    @Modifying
    @Query(value = "UPDATE ProductSku p SET p.sortHot=?2 WHERE p.skuId IN (?1)")
    int updateProductSkuHot(ArrayList<Long> pids, Boolean hot);

    /**
     * 添加销售记录
     * @param skuId
     * @param quantity
     */
    @Transactional
    @Modifying
    @Query(value = "UPDATE PRODUCT_SKU SET SALE_COUNT = SALE_COUNT + ?2 WHERE SKU_ID = ?1",nativeQuery = true)
    void updateSaleCount(Long skuId, Integer quantity);

    @Query(value = "SELECT DISTINCT MERCHANT_ID FROM PRODUCT_SKU WHERE SKU_ID IN (?1)" , nativeQuery = true)
    List<BigInteger> findStoreIdsBySkuIds(Collection<Long> skuIds);
}
